package com.example.spinning.controller;

import com.example.spinning.bean.PaginationResult;
import com.example.spinning.bean.RespBean;
import com.example.spinning.bean.SemiProductQuality;
import com.example.spinning.service.SemiQualityService;
import com.example.spinning.utils.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/semiProduct")
public class SemiQualityController {
    @Autowired
    SemiQualityService semiQualityService;

    //增加记录
    @Log(module = "半成品质量",description = "增加半成品质量信息")
    @RequestMapping(value = "/add/", method = RequestMethod.POST)
    public RespBean addNewSemiQuality( SemiProductQuality semiProductQuality) {
        if ("".equals(semiProductQuality.getName())|| semiProductQuality.getName() == null) {
            return new RespBean("error", "品名不能为空!");
        }
        if ("".equals(semiProductQuality.getBatch_no())|| semiProductQuality.getBatch_no() == null) {
            return new RespBean("error", "批号不能为空!");
        }
        int result = semiQualityService.addNewSemiQuality(semiProductQuality);
        if (result == 1) {
            return new RespBean("success",  "半成品信息添加成功");
        } else {
            return new RespBean("error","添加 半成品信息成功");
        }
    }

    //根据id删除记录
    @Log(module = "半成品质量",description = "删除半成品质量信息")
    @RequestMapping(value = "/delete/", method = RequestMethod.DELETE)
    public RespBean deleteById(Long[] ids) {
        boolean result = semiQualityService.deleteSemiQualityById(ids);
        if (result) {
            return new RespBean("success", "删除成功!");
        }
        return new RespBean("error", "删除失败!");
    }

    //查找
    @RequestMapping(value = "/getByMix/", method = RequestMethod.GET)
    public Map<String, Object> findPage(Integer page, Integer size,String batch_no,String name, Date date){
        PaginationResult pr = semiQualityService.findPage(page, size,batch_no,name,date);
        Map<String, Object> map = new HashMap<>();
        map.put("totleCount",pr.getTotal());
        map.put("list",pr.getList());
        return map;

    }

    @Log(module = "半成品质量",description = "修改半成品质量信息")
    @RequestMapping(value = "/update/",method = RequestMethod.PUT)
    public RespBean updateById(SemiProductQuality semiProductQuality){

        int result = semiQualityService.updateCategoryById(semiProductQuality);
        if (result == 1) {
            return new RespBean("success",  "半成品信息修改成功");
        } else {
            return new RespBean("error","半成品信息修改失败");
        }
    }



}
